from agent import agent
from packetholder import packetholder
from packet import packet

class source(agent):
  holder  = None

  output = None
  target = -1

  countdown = 0

  def __init__(self):
    super(source, self).__init__()
    
    self.holder = packetholder()
    self.holder.max_flits = 100

  def accept(self, src_node, inc_pkt, max_trans_f, route_hint=None):
    return 0

  def link_out(self, agent_out):
    self.output = agent_out

  def process(self):
    if(len(self.holder.contents) < 1):
      self.countdown -= 1

      if(self.countdown <= 0):
        print "Source Issuing Packet"
        new_pkt = packet(5)
        new_pkt.src = self.node_id
        new_pkt.dest = self.target

        self.holder.contents.append(new_pkt)

        self.countdown = 10
    else:
      for pkt in self.holder.contents:
        self.transmit(self.output, pkt, 1)
 
  def commit(self):
    self.holder.commit()

  def to_string(self):
    for pkt in self.holder.contents:
      return str(pkt.last_f - pkt.first_f)
    return "0"
